﻿//
// Lightbox Gallery Module for DotNetNuke
// Project Contributors - Will Strohl (http://www.WillStrohl.com), Armand Datema (http://www.schwingsoft.com)
//
//Copyright (c) 2009-2013, Will Strohl
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other 
//materials provided with the distribution.
//
//Neither the name of Will Strohl, Armand Datema, Lightbox Gallery, nor the names of its contributors may be 
//used to endorse or promote products derived from this software without specific prior 
//written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
//TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
//DAMAGE.
//

//INSTANT C# NOTE: Formerly VB project-level imports:
using System;
using System.Data;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Common.Utilities;
using Microsoft.ApplicationBlocks.Data;

namespace WillStrohl.Modules.Lightbox
{

	public class SqlDataProvider : DataProvider
	{

#region  Private Members 

		private const string ProviderType = "data";

		private ProviderConfiguration p_providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
		private string p_connectionString = string.Empty;
		private string p_providerPath = string.Empty;
		private string p_objectQualifier = string.Empty;
		private string p_databaseOwner = string.Empty;

		// local string values
		private const string c_ConnectionStringName = "connectionStringName";
		private const string c_ConnectionString = "connectionString";
		private const string c_ProviderPath = "providerPath";
		private const string c_ObjectQualifier = "objectQualifier";
		private const string c_Underscore = "_";
		private const string c_DatabaseOwner = "databaseOwner";
		private const string c_Period = ".";
		private const string c_SProc_Prefix = "wns_lightbox_";

		// sproc names
		private const string c_AddLightbox = "AddLightbox";
		private const string c_UpdateLightbox = "UpdateLightbox";
		private const string c_DeleteLightbox = "DeleteLightbox";
		private const string c_GetLightbox = "GetLightbox";
		private const string c_GetLightboxes = "GetLightboxes";
		private const string c_GetLightboxIds = "GetLightboxIds";
		private const string c_GetLightboxCount = "GetLightboxCount";
		private const string c_UpdateDisplayOrder = "UpdateDisplayOrder";
		private const string c_DoesDisplayOrderNeedUpdate = "DoesDisplayOrderNeedUpdate";

		private const string c_AddSetting = "AddSetting";
		private const string c_UpdateSetting = "UpdateSetting";
		private const string c_DeleteSetting = "DeleteSetting";
		private const string c_GetSettings = "GetSettings";

		private const string c_AddImage = "AddImage";
		private const string c_DeleteImageByFileName = "DeleteImageByFileName";
		private const string c_DeleteImageById = "DeleteImageById";
		private const string c_GetImageByFileName = "GetImageByFileName";
		private const string c_GetImageById = "GetImageById";
		private const string c_GetImages = "GetImages";
		private const string c_UpdateImage = "UpdateImage";
		private const string c_GetImageCount = "GetImageCount";

#endregion

#region  Constructors 

		/// <summary>
		/// Constructor
		/// </summary>
		/// <remarks></remarks>
		/// <history>
		/// 20101022 - wstrohl - Changed the connection string to use a different method of retrieval
		/// </history>
		public SqlDataProvider()
		{

			// Read the configuration specific information for this provider
			var objProvider = (Provider)(p_providerConfiguration.Providers[p_providerConfiguration.DefaultProvider]);

			// Read the attributes for this provider
			if (! (string.IsNullOrEmpty(objProvider.Attributes[c_ConnectionStringName])))
			{
				p_connectionString = Config.GetConnectionString();
			}
			else
			{
				p_connectionString = objProvider.Attributes[c_ConnectionString];
			}

			p_providerPath = objProvider.Attributes[c_ProviderPath];

			p_objectQualifier = objProvider.Attributes[c_ObjectQualifier];
			if (! (string.IsNullOrEmpty(p_objectQualifier)) && p_objectQualifier.EndsWith(c_Underscore) == false)
			{
				p_objectQualifier = string.Concat(p_objectQualifier, c_Underscore);
			}

			// Add willstrohl_ to the beginning of the sprocs
			p_objectQualifier = string.Concat(p_objectQualifier, c_SProc_Prefix);

			p_databaseOwner = objProvider.Attributes[c_DatabaseOwner];
			if (! (string.IsNullOrEmpty(p_databaseOwner)) && p_databaseOwner.EndsWith(c_Period) == false)
			{
				p_databaseOwner = string.Concat(p_databaseOwner, c_Period);
			}

		}

#endregion

#region  Properties 

		public string ConnectionString
		{
			get
			{
				return p_connectionString;
			}
		}

		public string ProviderPath
		{
			get
			{
				return p_providerPath;
			}
		}

		public string ObjectQualifier
		{
			get
			{
				return p_objectQualifier;
			}
		}

		public string DatabaseOwner
		{
			get
			{
				return p_databaseOwner;
			}
		}

#endregion

#region  Data Provider Implementation 

        public override int AddLightbox(int ModuleId, string GalleryName, string GalleryDescription, string GalleryFolder, int DisplayOrder, bool HideTitleDescription, int LastUpdatedBy, int GroupId)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_AddLightbox), ModuleId, GetNull(GalleryName), GetNull(GalleryDescription), GetNull(GalleryFolder), DisplayOrder, HideTitleDescription, LastUpdatedBy, GetNull(GroupId)));
		}
		public override void DeleteLightbox(int LightboxId)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_DeleteLightbox), LightboxId);
		}
		public override IDataReader GetLightboxes(int ModuleId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_GetLightboxes), ModuleId));
		}
		public override IDataReader GetLightbox(int LightboxId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_GetLightbox), LightboxId));
		}
		public override IDataReader GetLightboxIds(int ModuleId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_GetLightboxIds), ModuleId));
		}
        public override void UpdateLightbox(int LightboxId, int ModuleId, string GalleryName, string GalleryDescription, string GalleryFolder, int DisplayOrder, bool HideTitleDescription, int LastUpdatedBy, int GroupId)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_UpdateLightbox), LightboxId, ModuleId, GetNull(GalleryName), GetNull(GalleryDescription), GetNull(GalleryFolder), DisplayOrder, HideTitleDescription, LastUpdatedBy, GetNull(GroupId));
		}
		public override int GetLightboxCount(int ModuleId)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_GetLightboxCount), ModuleId));
		}
		public override void UpdateDisplayOrder()
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, string.Concat(DatabaseOwner, ObjectQualifier, c_UpdateDisplayOrder));
		}
		public override bool DoesDisplayOrderNeedUpdate()
		{
			return Convert.ToBoolean(SqlHelper.ExecuteScalar(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_DoesDisplayOrderNeedUpdate)));
		}


		public override int AddSetting(int LightboxId, int Padding, int Margin, bool Opacity, bool Modal, bool Cyclic, bool OverlayShow, decimal OverlayOpacity, string OverlayColor, bool TitleShow, string TitlePosition, string Transition, int Speed, int ChangeSpeed, bool ShowCloseButton, bool ShowNavArrows, bool EnableEscapeButton, string OnStart, string OnCancel, string OnComplete, string OnCleanup, string OnClosed)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_AddSetting), LightboxId, Padding, Margin, Opacity, Modal, Cyclic, OverlayShow, OverlayOpacity, OverlayColor, TitleShow, TitlePosition, Transition, Speed, ChangeSpeed, ShowCloseButton, ShowNavArrows, EnableEscapeButton, OnStart, OnCancel, OnComplete, OnCleanup, OnClosed));
		}
		public override void UpdateSetting(int SettingId, int LightboxId, int Padding, int Margin, bool Opacity, bool Modal, bool Cyclic, bool OverlayShow, decimal OverlayOpacity, string OverlayColor, bool TitleShow, string TitlePosition, string Transition, int Speed, int ChangeSpeed, bool ShowCloseButton, bool ShowNavArrows, bool EnableEscapeButton, string OnStart, string OnCancel, string OnComplete, string OnCleanup, string OnClosed)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_UpdateSetting), SettingId, LightboxId, Padding, Margin, Opacity, Modal, Cyclic, OverlayShow, OverlayOpacity, OverlayColor, TitleShow, TitlePosition, Transition, Speed, ChangeSpeed, ShowCloseButton, ShowNavArrows, EnableEscapeButton, OnStart, OnCancel, OnComplete, OnCleanup, OnClosed);
		}
		public override void DeleteSetting(int SettingId)
		{
			SqlHelper.ExecuteScalar(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_DeleteSetting), SettingId);
		}
		public override IDataReader GetSettings(int LightboxId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_GetSettings), LightboxId));
		}


		public override int AddImage(int LightboxId, string FileName, string Title, string Description, int DisplayOrder, int LastUpdatedBy)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_AddImage), LightboxId, FileName, Title, Description, DisplayOrder, LastUpdatedBy));
		}
		public override void DeleteImageByFileName(int LightboxId, string FileName)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_DeleteImageByFileName), LightboxId, FileName);
		}
		public override void DeleteImageById(int ImageId)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_DeleteImageById), ImageId);
		}
		public override IDataReader GetImageByFileName(int LightboxId, string FileName)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_GetImageByFileName), LightboxId, FileName));
		}
		public override IDataReader GetImageById(int ImageId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_GetImageById), ImageId));
		}
		public override IDataReader GetImages(int LightboxId)
		{
			return (IDataReader)(SqlHelper.ExecuteReader(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_GetImages), LightboxId));
		}
		public override void UpdateImage(int ImageId, int LightboxId, string FileName, string Title, string Description, int DisplayOrder, int LastUpdatedBy)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_UpdateImage), ImageId, LightboxId, FileName, Title, Description, DisplayOrder, LastUpdatedBy);
		}
		public override int GetImageCount(int LightboxId)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, string.Concat(this.DatabaseOwner, this.ObjectQualifier, c_GetImageCount), LightboxId));
		}

#endregion

#region  Private Helper Methods 

		private object GetNull(object Field)
		{
			return Null.GetNull(Field, DBNull.Value);
		}

#endregion

		}

}